package com.niannian.adapter;

import java.util.ArrayList;

import com.niannian.MyApplication;
import com.niannian.R;
import com.niannian.activity.FamilyNewsDetailActivity;
import com.niannian.bean.NewsBean;
import com.niannian.db.MyDBUser;
import com.niannian.util.Common;
import com.niannian.util.PlayerManager;
import com.niannian.util.UserInfoManager;
import com.niannian.util.VAR;
import com.niannian.view.CircleImageView;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class NewsAdapter extends BaseAdapter {
	protected MyDBUser userInfoManager = UserInfoManager.getUserInfoInstance();
	private ArrayList<NewsBean> ndatas;
	private LayoutInflater inflater;
	private Activity mActivity;
	BaseAdapter adapter;

	public NewsAdapter(Activity activity) {
		super();
		this.mActivity = activity;
		inflater = LayoutInflater.from(mActivity);
		this.ndatas = new ArrayList<NewsBean>();
	}

	@Override
	public int getCount() {
		return ndatas.size();
	}

	@Override
	public Object getItem(int arg0) {
		return ndatas.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	public ArrayList<NewsBean> getNDatasList() {
		return ndatas;
	}

	public void setAdapter(BaseAdapter adapter) {
		this.adapter = adapter;
	}

	View curView;
	AnimationDrawable ad;

	@Override
	public View getView(final int position, View v, ViewGroup arg2) {
		ViewHolder holder = null;
		if (v == null) {
			v = inflater.inflate(R.layout.news_item, null);
			holder = new ViewHolder();
			holder.ll_item_head = (LinearLayout) v.findViewById(R.id.ll_item_head);
			holder.riv_avatar = (CircleImageView) v.findViewById(R.id.riv_avatar);
			holder.tv_user_name = (TextView) v.findViewById(R.id.tv_user_name);
			holder.tv_news_info = (TextView) v.findViewById(R.id.tv_news_info);

			holder.ll_news_aud = (LinearLayout) v.findViewById(R.id.ll_news_aud);
			holder.tv_aud_len = (TextView) v.findViewById(R.id.tv_aud_len);
			holder.btn_play = (RelativeLayout) v.findViewById(R.id.btn_play);
			holder.iv_status_play = (ImageView) v.findViewById(R.id.iv_status_play);
			holder.pb_aud_loading = (ProgressBar) v.findViewById(R.id.pb_aud_loading);

			holder.tv_time = (TextView) v.findViewById(R.id.tv_time);
			holder.tv_news_parent_info = (TextView) v.findViewById(R.id.tv_news_parent_info);
			holder.iv_news_parent_pic = (ImageView) v.findViewById(R.id.iv_news_parent_pic);
			holder.iv_news_parent_aud = (ImageView) v.findViewById(R.id.iv_news_parent_aud);
			v.setTag(holder);
		} else {
			holder = (ViewHolder) v.getTag();
		}
		final NewsBean Ndata = ndatas.get(position);
		MyApplication.finalbitmap.display(holder.riv_avatar, Common.getAvatarThumbs(Ndata.getAuthor_avatar()));
		holder.tv_user_name.setText(Ndata.getAuthor_nickname());
		if (!Common.empty(Ndata.getCmt_info())) {
			holder.tv_news_info.setText(Ndata.getCmt_info());
			holder.tv_news_info.setVisibility(View.VISIBLE);
			holder.ll_news_aud.setVisibility(View.GONE);
		} else {
			holder.tv_aud_len.setText(Common.showAudLen(Ndata.getCmt_aud_len()));

			holder.tv_news_info.setVisibility(View.GONE);
			holder.ll_news_aud.setVisibility(View.VISIBLE);

			// 播放视图显示
			if (PlayerManager.getAudioPlayer(mActivity).currPlayingPath.equals(Ndata.getCmt_aud())) {

				if (PlayerManager.getAudioPlayer(mActivity).isPlayStatus == 1) {

					holder.iv_status_play.setBackgroundResource(R.anim.family_status_play_comment_anim);
					ad = (AnimationDrawable) holder.iv_status_play.getBackground();
					holder.iv_status_play.getViewTreeObserver().addOnPreDrawListener(opdl);

				} else if (PlayerManager.getAudioPlayer(mActivity).isPlayStatus == 2) {
					holder.iv_status_play.setBackgroundResource(R.drawable.status_pause_comment);
				} else {
					holder.iv_status_play.setBackgroundResource(R.drawable.play_comment_3);
				}
			} else {
				holder.iv_status_play.setBackgroundResource(R.drawable.play_comment_3);
			}

		}

		// 语音加载过程
		if (PlayerManager.getAudioPlayer(mActivity).DownloadingMap.containsKey(Ndata.getCmt_aud())) {
			holder.pb_aud_loading.setVisibility(View.VISIBLE);
		} else {
			holder.pb_aud_loading.setVisibility(View.GONE);
		}
		holder.tv_time.setText(Common.sgmdate(Ndata.getCreated_time()));

		if (!Common.empty(Ndata.getPost_pic())) {
			holder.iv_news_parent_pic.setVisibility(View.VISIBLE);
			holder.tv_news_parent_info.setVisibility(View.GONE);
			holder.iv_news_parent_aud.setVisibility(View.GONE);
			MyApplication.finalbitmap.display(holder.iv_news_parent_pic, Common.getPublicImagesSmallUrl(Ndata.getPost_pic()));
		} else if (!Common.empty(Ndata.getPost_info())) {
			holder.tv_news_parent_info.setText(Ndata.getPost_info());
			holder.iv_news_parent_pic.setVisibility(View.GONE);
			holder.tv_news_parent_info.setVisibility(View.VISIBLE);
			holder.iv_news_parent_aud.setVisibility(View.GONE);
		} else {
			holder.iv_news_parent_pic.setVisibility(View.GONE);
			holder.tv_news_parent_info.setVisibility(View.GONE);
			holder.iv_news_parent_aud.setVisibility(View.VISIBLE);

		}

		holder.btn_play.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (System.currentTimeMillis() - VAR.ExitTime > VAR.INTERVAL) {
					PlayerManager.getAudioPlayer(mActivity).aPlayer(adapter, Ndata.getCmt_aud());
					notifyDataSetChanged();
					VAR.ExitTime = System.currentTimeMillis();
				}
			}
		});

		holder.ll_item_head.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (System.currentTimeMillis() - VAR.ExitTime > VAR.INTERVAL) {
					Intent intent = new Intent(mActivity, FamilyNewsDetailActivity.class);
					intent.putExtra("pid", Ndata.getPost());
					mActivity.startActivity(intent);
					mActivity.overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
					VAR.ExitTime = System.currentTimeMillis();
				}

			}
		});

		return v;
	}

	OnPreDrawListener opdl = new OnPreDrawListener() {
		@Override
		public boolean onPreDraw() {
			ad.start();
			return true;
		}
	};

	class ViewHolder {
		LinearLayout ll_item_head;
		CircleImageView riv_avatar;
		TextView tv_user_name;
		TextView tv_news_info;
		LinearLayout ll_news_aud;

		ProgressBar pb_aud_loading;
		TextView tv_time;
		TextView tv_news_parent_info;
		ImageView iv_news_parent_pic;

		ImageView iv_news_parent_aud;

		TextView tv_aud_len;
		RelativeLayout btn_play;
		ImageView iv_status_play;

	}

}
